package com.jeesite.modules.aliyun.msg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.jeesite.common.io.PropertiesUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.mapper.JsonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;


public class AliyunSmsUtils {

    private final static Logger logger = LoggerFactory.getLogger(AliyunSmsUtils.class);

    /**
     * 发送阿里大于短信
     * @param content 短信内容
     * @param mobile 接受手机号码
     */
    public static String sendByTemplate(String mobile, String templateCode, String templateParam) {
        PropertiesUtils props = PropertiesUtils.getInstance();
        String accessKeyId = props.getProperty("msg.sms.accessKeyId");
        String accessKeySecret = props.getProperty("msg.sms.accessKeySecret");
        String signName = props.getProperty("msg.sms.signName");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        boolean isSuccess = false;
        String msg = "";
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject jsonObject = (JSONObject) JSON.parse(response.getData());
            if(jsonObject.get("Code").equals("OK")){
                isSuccess = true;
            }
            msg = (String) jsonObject.get("Message");
        }  catch (ClientException e) {
            e.printStackTrace();
        }
        if(isSuccess) {
            logger.info("发送短信成功！");
            return "{result:0,message:\"发送短信成功！\"}";
        }else{
            logger.info("发送短信失败！原因{}", msg);
            return String.format("{result:1,message:\"发送短信失败！%s\"}", msg);
        }
    }


//发送短信
    public static void sendMessages(String mobile, String tpl,Map<String, Object> tplData){
        String result = sendByTemplate(mobile, tpl, JSON.toJSONString(tplData));
        Map<String, Object> map = JsonMapper.fromJson(result, Map.class);
        // 发送成功
        if (ObjectUtils.toInteger(map.get("result")) == 0){
        }//			 发送失败
        else{
            logger.error("短信发送失败{0}", result);
        }
    }
}
